<template>
  <div class="login_back">
    <div class="login_box">
      <!-- 图标 -->
      <div class="login_img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 输入框 -->
      <el-form :model="loginForm" ref="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="precheck">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button></el-form-item
        >
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //   重置表单
    reset() {
      this.$refs.loginForm.resetFields();
    },
    // 登陆前预校验
    precheck() {
      this.$refs.loginForm.validate(async (valid) => {
        console.log(valid);
        if (!valid) {
          return;
        }
        const { data: res } = await this.$http.post("login", this.loginForm);
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg);
        }
        this.$message.success(res.meta.msg);
        // 1.登陆成功后将服务器分配的session存储在sessionStorage中
        window.sessionStorage.setItem("token", res.data.token);
        // 2.通过编程式导航跳转页面到/home
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang='less' scoped>
.login_back {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #eee;
    position: relative;
    .login_img {
      height: 130px;
      width: 130px;
      border: 1px solid gray;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px gray;
      position: absolute;
      left: 50%;
      background-color: #eee;
      transform: translate(-50%, -50%);
      img {
        height: 130px;
        width: 130px;
        border-radius: 50%;
        background-color: #fff;
      }
    }
    .el-form {
      margin-top: 120px;
      padding: 0 10px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>